package com.heer.gymnasium.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;

import static android.R.attr.targetSdkVersion;

/**
 * Created by yan on 2018/1/19.
 */

public class PermissionUtils {

    public static final int PERMISSION_CODE_5 = 5;
    public static final int PERMISSION_CODE_6 = 6;

    /**
     * @param permission
     * @return
     */
    public static boolean selfPermissionGranted(Context context, String permission) {
        // For Android < Android M, self permissions are always granted.
        boolean result = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (targetSdkVersion >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                result = context.checkSelfPermission(permission)
                        != PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                result = PermissionChecker.checkSelfPermission(context, permission)
                        != PermissionChecker.PERMISSION_GRANTED;
            }
        }

        return result;
    }

    public static void openPermission(Activity context, String[] permissions,int requestCode) {
        ActivityCompat.requestPermissions(context,
                permissions,
                requestCode);
    }
}
